#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "jiexi.h"
#include "wrizhi.h"
ssize_t my_read(int fd, char *buf, size_t count) {
    ssize_t newlen;
    ssize_t len = read(fd, buf, count);
    wrizhi("myread-len:%ld\n", len);
    while (len < count) {
        if (len > 0) {
            newlen = read(fd, buf + len, count - len);
            len += newlen;
            wrizhi("myread-len:%ld\n", len);
        } else {
            wrizhi("my_read:fault!\n");
            return -1;
        }
    }
    wrizhi("myread-zong-len:%ld\n", len);
    return len;
}
enum ml get_enum(char *s) {
    if (strncmp(s, "ls", 2) == 0) {
        return ls;
    } else if (strncmp(s, "quit", 4) == 0) {
        return quit;
    } else if (strncmp(s, "get", 3) == 0) {
        return get;
    } else if (strncmp(s, "put", 3) == 0) {
        return put;
    } else if (strncmp(s, "cd", 2) == 0) {
        return cd;
    } else if (strncmp(s, "history", 7) == 0) {
        return history;
    } else {
        return erro;
    }
}
file jiexi(char *s, file ff) {
    ff.s = get_enum(s);
    strtok(s, " ");
    char *p = strtok(NULL, " ");
    if (p == NULL) {
        memset(ff.name, 0, sizeof(ff.name));
        return ff;
    }
    strcpy(ff.name, p);
    return ff;
}
void fasong(int fd, file ff) {
    char s[132] = "md5sum ";
    char *p = NULL;
    char *sub = NULL;
    p = strcat(s, ff.name);
    FILE *fp = popen(p, "r");
    fread(ff.md5, 1, sizeof(ff.md5), fp);
    pclose(fp);

    fp = fopen(ff.name, "r");
    fseek(fp, 0, SEEK_END);
    ff.len = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    sub = (char *)malloc(sizeof(char) * ff.len);
    memset(sub, 0, ff.len);
    fread(sub, 1, ff.len, fp);
    fclose(fp);
    printf("%s\n", ff.md5);
    write(fd, (char *)&ff, sizeof(ff));
    write(fd, sub, ff.len);
    my_read(fd, ff.md5, sizeof(ff.md5));
    printf("%s\n", ff.md5);
    free(sub);
}
void huoqu(int fd, file ff) {
    write(fd, &ff, sizeof(ff));
    my_read(fd, (char *)&ff, sizeof(ff));
    char *sub = NULL;
    sub = (char *)malloc(sizeof(char) * ff.len);
    memset(sub, 0, ff.len);
    my_read(fd, sub, ff.len);
    printf("%s\n", ff.md5);
    FILE *fp = fopen(ff.name, "w+");
    fwrite(sub, 1, ff.len, fp);
    fclose(fp);
    free(sub);

    char s[135] = "md5sum ";
    char *p;
    p = strcat(s, ff.name);
    fp = popen(p, "r");
    fread(ff.md5, 1, sizeof(ff.md5), fp);
    pclose(fp);
    printf("%s\n", ff.md5);
}
char *zhuanghuan(enum ml sss) {
    char *p = NULL;
    p = (char *)malloc(sizeof(char) * 8);
    memset(p, 0, 8);
    switch (sss) {
        case 1:
            strcpy(p, "ls");
            break;
        case 2:
            strcpy(p, "quit");
            break;
        case 3:
            strcpy(p, "get");
            break;
        case 4:
            strcpy(p, "put");
            break;
        case 5:
            strcpy(p, "cd");
            break;
        case 6:
            strcpy(p, "history");
            break;
        default:
            strcpy(p, "erro");
            break;
    }
    return p;
}

void gethist(int fd) {
    hist p;
    printf("############################\n");
    while (1) {
        my_read(fd, (char *)&p, sizeof(p));
        printf("命令：%s参数：%s\n", zhuanghuan(p.hist_s), p.hist_name);
        if (p.next == NULL) {
            break;
        }
    }
    printf("############################\n");
}
int chuli(int fd, file ff) {
    long i;
    file ff_hist;
    char *sub = NULL;
    switch (ff.s) {
        case 1:
            write(fd, (char *)&ff, sizeof(file));
            my_read(fd, (char *)&ff, sizeof(file));
            sub = (char *)malloc(sizeof(char) * ff.len);
            memset(sub, 0, ff.len);
            my_read(fd, (char *)sub, ff.len);
            printf("%s", sub);
            free(sub);
            break;
        case 2:
            write(fd, &ff, sizeof(ff));
            close(fd);
            return -1;
            break;
        case 3:
            huoqu(fd, ff);
            break;
        case 4:
            fasong(fd, ff);
            break;
        case 5:
            write(fd, &ff, sizeof(ff));
            break;
        case 6:
            write(fd, &ff, sizeof(ff));
            gethist(fd);
            break;
        default:
            printf("无效命令\n");
            break;
    }
    printf("-----------------\n");
    return 0;
}
